package com.wandian.texas.command.texas;

import java.util.List;

import com.wandian.command.Cmd;
import com.wandian.command.UserCommand;
import com.wandian.texas.BaseCommandObject;
import com.wandian.texas.UserMgr;
import com.wandian.texas.db.DBMgr;
import com.wandian.texas.db.DaoManager;
import com.wandian.texas.entity.AnnounceEntity;
import com.wandian.texas.entity.UserInfoEntity;
import com.wandian.texas.protocol.Protocol;
import com.wandian.util.Log;

/**
 * @author long
 * @Create 2012-10-27 下午12:08:50
 * @QQ 108232706
 * @version 1.0
 * @description LobbyCmd
 */
@Cmd(code = Protocol.LobbyCmd, desc = "")
public class LobbyCmd extends UserCommand {
	@Override
	public void execute(BaseCommandObject cmdObject) {
		super.execute(cmdObject);
		short cmdId = cmdObject.readShort();
		cmdObject.setCmd(cmdId);
		switch (cmdId) {
		case Protocol.CMD_GameMessageList:// 公告
			// cmdObject.setCmd(Protocol.CMD_GameMessageList);
			Log.debug("[LobbyCmd.CMD_GameMessageList]");
			sendGameMessage(cmdObject);
			break;
		case Protocol.CMD_USE_PROP:
			CMD_USE_PROP(cmdObject);
			break;
		case Protocol.CMD_CHANGE_SEATICO:
			CMD_CHANGE_SEATICO(cmdObject);
			break;
		case Protocol.CMD_CHANGE_HEADIMAGE:
			CMD_CHANGE_HEADIMAGE(cmdObject);
			break;
		default:
			break;
		}

	}

	private void CMD_CHANGE_HEADIMAGE(BaseCommandObject cmdObject) {
		// 保存头像
		int byteLen = cmdObject.readInt();
		byte result = 1;
		if (byteLen > 0) {
			byte[] headImage = new byte[byteLen];
			cmdObject.readBytes(headImage, 0, headImage.length);
			result = DaoManager.GameRoomDao(cmdObject.dbIndex).executeUpdate("sp_UpdateUserHeadImage",  DBMgr.TexasPokerDB, cmdObject.getUser().userId, headImage) ? (byte) 0 : (byte) 1;
		}
		cmdObject.writeBegin();
		cmdObject.writeByte(result);
		cmdObject.writeEnd();
		UserMgr.I().SendData(cmdObject);
	}

	private void CMD_CHANGE_SEATICO(BaseCommandObject cmdObject) {
		int pframe = cmdObject.readInt();
		UserInfoEntity user = cmdObject.getUser();
		cmdObject.writeBegin();
		if (DaoManager.UserInfoDao(cmdObject.dbIndex).exeSql("UPDATE PlayerInfo SET  icon=? WHERE UserId=? ",DBMgr.TexasPokerDB, pframe, user.userId)) {
			cmdObject.writeByte((byte) 0);
			user.icon = String.valueOf(pframe);
		} else {
			cmdObject.writeByte((byte) 1);
		}
		cmdObject.writeInt(pframe);
		cmdObject.writeEnd();
		UserMgr.I().SendData(cmdObject);
	}

	private void CMD_USE_PROP(BaseCommandObject cmdObject) {
		int userid = cmdObject.readInt();
		int pcate = cmdObject.readInt();
		int pframe = cmdObject.readInt();
		int type = cmdObject.readInt();
		cmdObject.writeBegin();
		cmdObject.writeInt(0);
		cmdObject.writeInt(0);
		// objData.errId=this._socket.readInt();
		// objData.useMid=this._socket.readUInt();
		// objData.gold=this._socket.readUInt();
		// objData.toMid=this._socket.readUInt();
		// objData.pcate=this._socket.readInt();
		// objData.pframe=this._socket.readInt();
		// objData.type=this._socket.readInt();
	}

	private void sendGameMessage(BaseCommandObject cmdObject) {
		List<AnnounceEntity> announces = DaoManager.LobbyDao(cmdObject.dbIndex).getAnnouce();
		int count = 0;
		cmdObject.writeBegin();
		if (announces != null && announces.size() > 0) {
			count = announces.size();
		}
		cmdObject.writeInt(count);
		int index = 0;
		AnnounceEntity info;
		while (index < count) {
			info = announces.get(index);
			cmdObject.writeInt(info.orderNum);
			cmdObject.writeString(info.title);
			cmdObject.writeString(info.announcement);
			index++;
		}
		cmdObject.writeEnd();
		UserMgr.I().SendData(cmdObject);
	}
}
